package com.qys.livesMall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.product.entity.H2ProductCategory;
import com.qys.livesMall.product.mapper.H2ProductCategoryMapper;
import com.qys.livesMall.product.service.H2ProductCategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gmx
 * @since 2023-05-17
 */
@Service
public class H2ProductCategoryServiceImpl extends ServiceImpl<H2ProductCategoryMapper, H2ProductCategory> implements H2ProductCategoryService {

    @Resource
    private H2ProductCategoryMapper h2ProductCategoryMapper;
    /***
     * 根据父ID查询子分类
     * @param pid
     * @return
     */

    @Override
    public List<H2ProductCategory> queryByParentId(Long pid) {
        QueryWrapper<H2ProductCategory> queryWrapper = new QueryWrapper<H2ProductCategory>();
        queryWrapper.lambda().eq(H2ProductCategory::getParentId,pid)
                .eq(H2ProductCategory::getEnable,1)
                .eq(H2ProductCategory::getIsDelete,0);
        return h2ProductCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public R deleteByids(List<H2ProductCategory> h2ShopCategories) {
        if (h2ShopCategories.size()==1){
            H2ProductCategory h2ProductCategory = h2ShopCategories.get(0);
            LambdaQueryWrapper<H2ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(H2ProductCategory::getId, h2ProductCategory.getId());
            h2ProductCategory.setIsDelete(1);
            int i= h2ProductCategoryMapper.update(h2ProductCategory,queryWrapper);
            if(i<1){
                return R.fail(MessageUtils.message("category.delete.error"));
            }
            return R.ok();
        }
        LambdaQueryWrapper<H2ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        Long[] ids = h2ShopCategories.stream().map(H2ProductCategory::getId).toArray(Long[]::new);
        int i= h2ProductCategoryMapper.deleteByids(ids);
        if(i<1){
            return R.fail(MessageUtils.message("category.delete.error"));
        }
        return R.ok();
    }
}
